package com.jiawa.train.member.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiawa.train.common.context.LoginMemberContext;
import com.jiawa.train.common.exception.BusinessException;
import com.jiawa.train.common.exception.BusinessExceptionEnum;
import com.jiawa.train.common.resp.PageResp;
import com.jiawa.train.member.domain.po.Passenger;
import com.jiawa.train.member.mapper.PassengerMapper;
import com.jiawa.train.member.req.PassengerQueryReq;
import com.jiawa.train.member.req.PassengerSaveReq;
import com.jiawa.train.member.resp.PassengerQueryResp;
import com.jiawa.train.member.service.IPassengerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 乘车人 服务实现类
 * </p>
 *
 * @author hrz
 * @since 2025-09-06
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class PassengerServiceImpl extends ServiceImpl<PassengerMapper, Passenger> implements IPassengerService {


    @Override
    public void save(PassengerSaveReq passengerSaveReq) {
        Passenger passenger = BeanUtil.copyProperties(passengerSaveReq, Passenger.class);
        if(passenger.getId() == null){
            //新增
            passenger.setMemberId(LoginMemberContext.getId()); //存储用户id
            passenger.setCreateTime(LocalDateTime.now());
            passenger.setUpdateTime(LocalDateTime.now());
            //保存起来
            baseMapper.insert(passenger);
        }else {
            //更新
            passenger.setUpdateTime(LocalDateTime.now());
            baseMapper.updateById(passenger);
        }
    }

    @Override
    public PageResp<PassengerQueryResp> queryList(PassengerQueryReq req) {
        //1.校验是否有id
        Page<Passenger> page = new Page<>();
        if(req.getMemberId() != null){
            log.info("用户端");
             page = this.lambdaQuery()
                    .eq(Passenger::getMemberId, req.getMemberId())
                     .orderByDesc(Passenger::getId)
                    .page(new Page<>(req.getPage(), req.getSize()));
        }else{
            //2.没有id,说明是admin查询,所有的数据
            log.info("管理员端");
            page = this.lambdaQuery()
                    .orderByDesc(Passenger::getId)
                    .page(new Page<>(req.getPage(), req.getSize()));
        }
        //3.返回结果
        List<Passenger> records = page.getRecords();
        if(CollUtil.isEmpty(records)){
            PageResp<PassengerQueryResp> objectPageResp = new PageResp<>();
            objectPageResp.setTotal(0L);
            objectPageResp.setList(new ArrayList<>());//返回空集合
            return objectPageResp;
        }
        List<PassengerQueryResp> list = BeanUtil.copyToList(records, PassengerQueryResp.class);
        PageResp<PassengerQueryResp> objectPageResp = new PageResp<>();
        objectPageResp.setTotal(page.getTotal());
        objectPageResp.setList(list);
        return objectPageResp;
    }

    @Override
    public void delete(Long id) {
        //1.校验参数
        if(ObjectUtil.isEmpty(id)){
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        }
        //2.删除
        baseMapper.deleteById(id);
    }

    @Override
    public List<PassengerQueryResp> queryMine() {
        Long id = LoginMemberContext.getId();
        //1.去数据库查询所有该会员的乘车人信息
        List<Passenger> list = this.lambdaQuery()
                .eq(Passenger::getMemberId, id)
                .orderByDesc(Passenger::getId)
                .list();
        //2.返回
        return BeanUtil.copyToList(list, PassengerQueryResp.class);
    }
}
